package ch.usi.inf.pf2;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;

import javax.swing.JComponent;

public class MegaInputDrawer extends JComponent implements java.io.Serializable{

	private PanicSimDrawingArea area;

	private ArrayList<Integer> drawingValue = new ArrayList<Integer>();

	private int oldX = 20;
	private int oldY = 25;
	private int actual = 0;
	private int n;

	public MegaInputDrawer(PanicSimDrawingArea area) {
		this.area = area;

	}
	
	public void setPos(int n) {
		this.n = n;
	}
	

	public void paintComponent(final Graphics g) {

		g.setColor(Color.WHITE);
		g.fillRect(5, 5, this.getParent().getWidth() - 5, this.getParent()
				.getHeight() - 5);

		g.setColor(Color.BLACK);

		for (int i = 0; i < drawingValue.size(); i = i + 4) {
			g.drawLine(drawingValue.get(i), drawingValue.get(i + 1),
					drawingValue.get(i + 2), drawingValue.get(i + 3));

		}

		if (area.getSelectedMegaInput() != null) {

			if (area.getSelectedMegaInput().getValue(n) == null) {
				drawingValue.add(oldX);
				drawingValue.add(25);
				drawingValue.add(oldX + 10);
				drawingValue.add(25);

				oldX = oldX + 10;
			}

			else if (area.getSelectedMegaInput().getValue(n) == 1
					&& area.getSelectedMegaInput().getValue(n) != actual) {

				drawingValue.add(oldX);
				drawingValue.add(15);
				drawingValue.add(oldX);
				drawingValue.add(25);

				oldY = 15;

				drawingValue.add(oldX);
				drawingValue.add(oldY);
				drawingValue.add(oldX + 10);
				drawingValue.add(oldY);

				actual = 1;

				oldX = oldX + 10;
			}

			else if (area.getSelectedMegaInput().getValue(n) == 0
					&& area.getSelectedMegaInput().getValue(n) != actual) {

				drawingValue.add(oldX);
				drawingValue.add(15);
				drawingValue.add(oldX);
				drawingValue.add(25);

				oldY = 25;

				drawingValue.add(oldX);
				drawingValue.add(oldY);
				drawingValue.add(oldX + 10);
				drawingValue.add(oldY);

				actual = 0;
				oldX = oldX + 10;
			}

			else if (area.getSelectedMegaInput().getValue(n) == actual) {

				drawingValue.add(oldX);
				drawingValue.add(oldY);
				drawingValue.add(oldX + 10);
				drawingValue.add(oldY);

				oldX = oldX + 10;
			}

			if (oldX > 650) {

				for (int i = 0; i < drawingValue.size(); i = i + 4) {

					drawingValue.set(i, drawingValue.get(i) - 10);
					drawingValue.set(i + 2, drawingValue.get(i + 2) - 10);
				}

				oldX = oldX - 10;
			}

		} else {
			oldX = 20;
			g.setColor(Color.WHITE);
			g.fillRect(5, 5, this.getParent().getWidth() - 5, this.getParent()
					.getHeight() - 5);

			drawingValue = new ArrayList<Integer>();
		}

		try {
			Thread.sleep(100);
		} catch (Exception e) {
		}
		
	}
}
